const app = getApp()
import {
  withdrawal,
  applyWithdrawal,
  walletWithdraw
} from '../../config/https'
//提现信息、佣金申请提现、钱包申请提现
Page({

  /**
   * 页面的初始数据
   */
  data: {
    withdrawType: '',       //提现类型
    withdrawalInfo: '',
    money: '',
    bankId: '',
    bankCardNumber: ''
  },
  getData(){
    withdrawal({
      token: wx.getStorageSync('token')
    }).then(res => {
      if(res.code == 1){
        this.setData({
          withdrawalInfo: res.data
        })
      }
    })
  },
  choiceBank(){
    wx.navigateTo({
      url: '/pages/mybankcard/mybankcard',
    })
  },
  inputMoney(e){
    this.setData({
      money: e.detail.value
    })
  },
  confirmSubmit(){
    let bankId = this.data.bankId
    let money = Number(this.data.money)
    let minMoney = Number(this.data.withdrawalInfo.min_withdraw)
    let myMoney = this.data.withdrawType == 1 ? Number(this.data.withdrawalInfo.balance) : Number(this.data.withdrawalInfo.money)
    if(!bankId){
      app.$tip('请先选择提现银行卡')
      return
    }
    if(money > myMoney){
      app.$tip('提现金额不可大于我的余额')
      return
    }
    if(money < minMoney){
      app.$tip('最低提现' + minMoney + '元')
      return
    }
    if(this.data.withdrawType == 2){
      walletWithdraw({
        token: wx.getStorageSync('token'),
        money: this.data.money,
        bank_id: this.data.bankId
      }).then(res => {
        app.$tip(res.msg)
        if(res.code == 1){
          this.setData({
            money: ''
          })
          this.getData()
        }
      })
      return
    }
    applyWithdrawal({
      token: wx.getStorageSync('token'),
      money: this.data.money,
      bank_id: this.data.bankId
    }).then(res => {
      app.$tip(res.msg)
      if(res.code == 1){
        this.setData({
          money: ''
        })
        this.getData()
      }
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      withdrawType: options.type
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    this.getData()
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})